package com.gmail.blackdog1987.carcare.adapter;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TCarfixshop;

public class CarFixAdapter extends BaseAdapter {

	List<TCarfixshop> datas;
	Context mContext;
	ImageLoader imageLoader;

	public CarFixAdapter(Context context, List<TCarfixshop> datas) {
		this.datas = datas;
		this.mContext = context;
		imageLoader = ImageLoaderFactory.create(mContext);
	};

	public void addDatas(List<TCarfixshop> datas) {
		this.datas.addAll(datas);
	}

	public void setDatas(List<TCarfixshop> datas) {
		if (this.datas == null) {
			this.datas = new ArrayList<TCarfixshop>();
		}
		this.datas.clear();
		this.datas.addAll(datas);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public TCarfixshop getItem(int position) {
		return datas == null ? null : datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final TCarfixshop m = getItem(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_carfix, null);
			holder = new ViewHolder();
			holder.mHead = (CubeImageView) convertView.findViewById(R.id.carfix_img);
			holder.mTitle = (TextView) convertView.findViewById(R.id.carfix_title);
			holder.mExt = (TextView) convertView.findViewById(R.id.carfix_ext);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mTitle.setText(m.getTitle());
		holder.mExt.setText(m.getLocation());

		if (m.headurl != null && m.headurl.length() != 0) {
			holder.mHead.loadImage(imageLoader, m.headurl);
		}
		convertView.setTag(holder);
		return convertView;
	}

	public class ViewHolder {
		TextView mTitle;
		TextView mExt;
		CubeImageView mHead;
	}

}
